import { createRouter, createWebHistory } from 'vue-router'
import store from "@/store";
import {notification} from "ant-design-vue";

const routes = [
  // {
  //   path: '/',
  //   name: 'home',
  //   component: HomeView
  // },
  // {  //大型项目可以采用懒加载的方式 提高编译的效率
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // },  //大型项目可以采用懒加载的方式 提高编译的效率
  {
    path: '/login',
    component: () => import('../views/LoginView.vue')
  },
  {
    path: '/',
    component: () => import('../views/MainVue.vue'),
    meta : {
      loginRequire : true  //自定义变量
    },
    children:[{
      path: 'welcome',
      component:() => import('../views/main/welcome.vue'),
    },{
      path: 'passenger',
      component:() => import('../views/main/passenger.vue')
    },{
      path: 'ticket',
      component:() => import('../views/main/ticket.vue')
    },{
      path: 'order',
      component:() => import('../views/main/order.vue')
    },{
      path: 'MyTicket',
      component:() => import('../views/main/my_ticket.vue')
    }]
  },
  {
    path: '',  //访问根域名 也就是/的时候 会重定向到welcome
    redirect: '/welcome'
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

/**
 * axios是和后端交互的拦截
 * 而router beforeEach是纯粹的前端路由跳转的拦截
 */
// 路由登录拦截
router.beforeEach((to, from, next) => {
  // 要不要对meta.loginRequire属性做监控拦截
  if (to.matched.some(function (item) {
    console.log(item, "是否需要登录校验：", item.meta.loginRequire || false);
    return item.meta.loginRequire
  })) { //如果前面是true 表示需要验证 就会执行下面内容 否则直接next 不执行验证
    const _member = store.state.member;
    console.log("页面登录校验开始：", _member);
    if (!_member.token) {
      console.log("用户未登录或登录超时！");
      notification.error({ description: "未登录或登录超时" });
      next('/login');
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router
